/**
 * 桌面ui控件封装
 */
var desktop=new Object();

desktop.icon_div="desktop";
desktop.dock_ul="dock";

//桌面控件id生成工具，可以生成唯一的id提供dom操作
desktop.UUID = function() {
};
desktop.UUID.prototype.type = "desktop.UUID";
desktop.UUID.create = function() {
	var _4a13 = function() {
		return (((1 + Math.random()) * 65536) | 0).toString(16).substring(1);
	};
	return (_4a13() + _4a13() + "-" + _4a13() + "-" + _4a13() + "-" + _4a13()
			+ "-" + _4a13() + _4a13() + _4a13());
};

/**
 * 桌面快捷图标控件封装，可以根据配置信息生成相关快捷图标在桌面显示
 * cfg配置信息，cfg.text显示的文字,cfg.icon图标路径
 */
desktop.DockIcon = function(cfg) {
	this.construct(cfg);
};

desktop.DockIcon.index=0;

desktop.DockIcon.prototype.getSeqNum=function(){
	return desktop.DockIcon.index++;
};

desktop.DockIcon.offset_h=80;

desktop.DockIcon.offset_w=80;

desktop.DockIcon.MAXROW_CNT=7;

desktop.DockIcon.prototype.construct = function(cfg) {
	this.id = desktop.UUID.create();
	this.seq= this.getSeqNum();
	this.text = cfg.text;
	this.icon = cfg.icon;
	this.href=cfg.href;
	this.html = this.createHtml();
	this.dockHtml = this.createDockHtml();
	$('#'+desktop.icon_div).append(this.html);
	$('#'+desktop.dock_ul).append(this.dockHtml);
	
	var clickHandler=function(){
		alert(this.id+' clicked');
	};
	if(cfg.clickHandler){
		$('#'+this.id).on('click',clickHandler);
	}
};

desktop.DockIcon.prototype.type = "desktop.DockIcon";

desktop.DockIcon.prototype.remove=function(){
	$('#'+this.id).remove();
	$('#'+this.href).remove();
};

desktop.DockIcon.prototype.createDockHtml=function(){
	var suffix=this.href.substring(this.href.lastIndexOf("_")+1);
	var html="<li id=\""+this.href+"\"><a href=\"#window_"+suffix+"\"> <img src=\""+this.icon+"\" />"+this.text+"</a></li>";
	return html;
};

desktop.DockIcon.prototype.createHtml=function(){
	var top=20+desktop.DockIcon.offset_h*(this.seq%7);
	var left=20+desktop.DockIcon.offset_w*parseInt(this.seq/7);
	var html="<a id=\""+this.id+"\" class=\"abs icon\" style=\"left: "+left+"px; top: "+top+"px;\"  href=\"#"+this.href+"\">";
	if(this.icon){
		html+="<img src=\""+this.icon+"\" />";
	}else{
		html+="<img src=\"${ctx}/images/icons/icon_32_drive.png\" />";
	}
	html+=this.text;
	html+="</a>";
	return html;
};